package com.edgegateway.web.controller.iot;

import java.util.List;

import com.edgegateway.common.core.domain.entity.SysUser;
import com.edgegateway.common.pk.IdWorker;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.edgegateway.common.annotation.Log;
import com.edgegateway.common.enums.BusinessType;
import com.edgegateway.system.domain.IotThingsModel;
import com.edgegateway.system.service.IIotThingsModelService;
import com.edgegateway.common.core.controller.BaseController;
import com.edgegateway.common.core.domain.AjaxResult;
import com.edgegateway.common.utils.poi.ExcelUtil;
import com.edgegateway.common.core.page.TableDataInfo;

/**
 * 物模型Controller
 * 
 * @author Doooxu
 * @date 2023-01-11
 */
@Controller
@RequestMapping("/system/model")
public class IotThingsModelController extends BaseController
{
    private String prefix = "iot/model";

    @Autowired
    private IIotThingsModelService iotThingsModelService;

    @Autowired
    private IdWorker idWorker;

    @RequiresPermissions("system:model:view")
    @GetMapping()
    public String model()
    {
        return prefix + "/model";
    }

    /**
     * 查询物模型列表
     */
    @RequiresPermissions("system:model:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IotThingsModel iotThingsModel)
    {
        startPage();
        List<IotThingsModel> list = iotThingsModelService.selectIotThingsModelList(iotThingsModel);
        return getDataTable(list);
    }

    /**
     * 查询物模型列表
     */
    @RequiresPermissions("system:model:list")
    @PostMapping("/getList")
    @ResponseBody
    public TableDataInfo getList(/*@PathVariable("deviceId") String deviceId*/)
    {
        IotThingsModel iotThingsModel = new IotThingsModel();
        //iotThingsModel.setProductId(deviceId);
        List<IotThingsModel> list = iotThingsModelService.selectIotThingsModelList(iotThingsModel);
        return getDataTable(list);
    }

    /**
     * 导出物模型列表
     */
    @RequiresPermissions("system:model:export")
    @Log(title = "物模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IotThingsModel iotThingsModel)
    {
        List<IotThingsModel> list = iotThingsModelService.selectIotThingsModelList(iotThingsModel);
        ExcelUtil<IotThingsModel> util = new ExcelUtil<IotThingsModel>(IotThingsModel.class);
        return util.exportExcel(list, "物模型数据");
    }

    /**
     * 新增物模型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存物模型
     */
    @RequiresPermissions("system:model:add")
    @Log(title = "物模型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IotThingsModel iotThingsModel)
    {
        iotThingsModel.setModelId(idWorker.nextId());
        return toAjax(iotThingsModelService.insertIotThingsModel(iotThingsModel));
    }

    /**
     * 修改物模型
     */
    @RequiresPermissions("system:model:edit")
    @GetMapping("/edit/{modelId}")
    public String edit(@PathVariable("modelId") String modelId, ModelMap mmap)
    {
        IotThingsModel iotThingsModel = iotThingsModelService.selectIotThingsModelByModelId(modelId);
        mmap.put("iotThingsModel", iotThingsModel);
        return prefix + "/edit";
    }

    /**
     * 修改保存物模型
     */
    @RequiresPermissions("system:model:edit")
    @Log(title = "物模型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IotThingsModel iotThingsModel)
    {
        return toAjax(iotThingsModelService.updateIotThingsModel(iotThingsModel));
    }

    /**
     * 删除物模型
     */
    @RequiresPermissions("system:model:remove")
    @Log(title = "物模型", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(iotThingsModelService.deleteIotThingsModelByModelIds(ids));
    }



}
